//
//  RBBannerView.m
//  SaltyHeart
//
//  Created by Lan on 2017/6/5.
//  Copyright © 2017年 ST. All rights reserved.
//

#import "RBBannerView.h"
#import "RBProxy.h"
#import "Masonry.h"

@interface RBBannerView ()<UIScrollViewDelegate, iCarouselDataSource, iCarouselDelegate>

@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, assign) NSInteger numberOfItems;
@property(nonatomic, strong) iCarousel *carousel;

@end

@implementation RBBannerView

#pragma mark - System

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.clipsToBounds = YES;
        _itemViewClass = [UIImageView class];
        self.duration = 3;
    }
    return self;
}

 - (void)dealloc
{
    [_timer invalidate];
}

#pragma mark - iCarouselDelegate

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.numberOfItems;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    UIImageView *imgView = (UIImageView *)view;
    if (!imgView)
    {
        imgView = [[self.itemViewClass alloc] initWithFrame:self.bounds];
        imgView.clipsToBounds = YES;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
    }
    imgView.frame = self.bounds;
    if ([self.deleagte respondsToSelector:@selector(bannerView:willSetupView:withIndex:)])
    {
        [self.deleagte bannerView:self willSetupView:imgView withIndex:index];
    }
    return imgView;
}

- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            return self.numberOfItems <= 1? NO: YES;
        }
            break;
        default:
            return value;
            break;
    }
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    if ([self.deleagte respondsToSelector:@selector(bannerView:didSelectView:withIndex:)])
    {
        [self.deleagte bannerView:self didSelectView:nil withIndex:index];
    }
}

- (void)carouselWillBeginDragging:(iCarousel *)carousel
{
    [self pauseTimer];
}

- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    if ([self.deleagte respondsToSelector:@selector(bannerView:scrollToIndex:)])
    {
        [self.deleagte bannerView:self scrollToIndex:[carousel currentItemIndex]];
    }
}

#pragma mark - Private

- (void)startTimer
{
    if (self.numberOfItems > 1)
    {
        self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:self.duration];
    }
    else
    {
        [self pauseTimer];
    }
}

- (void)pauseTimer
{
    _timer.fireDate = [NSDate distantFuture];
}

#pragma mark - Public

- (UIView *)viewWithIndex:(NSInteger)index
{
    return [self.carousel itemViewAtIndex:index];
}

- (void)reloadData
{
    self.carousel.scrollEnabled = self.numberOfItems > 1;
    [self.carousel reloadData];
    [self pauseTimer];
    if (self.numberOfItems >= 1)
    {
        [self.carousel scrollToItemAtIndex:0 animated:NO];
        [self startTimer];
    }
}

#pragma mark - Event

- (void)scrollImageView
{
    NSInteger index = [self.carousel currentItemIndex] + 1;
    if (index >= self.numberOfItems) {
        index = 0;
    }
    [self.carousel scrollToItemAtIndex:index animated:YES];
}

#pragma mark - Setter

- (void)setItemViewClass:(Class)itemViewClass
{
    _itemViewClass = itemViewClass;
    [self reloadData];
}

- (void)setDeleagte:(id<RBBannerViewDelegate>)deleagte
{
    _deleagte = deleagte;
    [self reloadData];
}

#pragma mark - Getter

- (NSInteger)numberOfItems
{
    if ([self.deleagte respondsToSelector:@selector(numberOfItemsInBannerView:)]) {
        return [self.deleagte numberOfItemsInBannerView:self];
    }else{
        return 0;
    }
}

- (iCarousel *)carousel
{
    if (!_carousel)
    {
        self.carousel = ({
            iCarousel *view = [iCarousel new];
            view.pagingEnabled = YES;
            view.dataSource = self;
            view.delegate = self;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _carousel;
}

- (NSTimer *)timer
{
    if (!_timer)
    {
        self.timer = ({
            NSTimer *timer = [NSTimer timerWithTimeInterval:self.duration target:[RBProxy proxyWithTarget:self] selector:@selector(scrollImageView) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
            timer;
        });
    }
    return _timer;
}

@end






